# Contributor: Christoph Reiter <reiter.christoph@gmail.com>

_realname=atomicwrites
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.3.0
pkgrel=2
pkgdesc="Atomic file writes on POSIX (mingw-w64)"
url="https://github.com/untitaker/python-atomicwrites"
license=('MIT')
arch=('any')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-pytest"
              "${MINGW_PACKAGE_PREFIX}-python2-pathlib2"
              "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
              "${MINGW_PACKAGE_PREFIX}-python3-pathlib2")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/untitaker/python-atomicwrites/archive/${pkgver}.tar.gz")
sha256sums=('e286509543a26777e8cb862c9373e89c31f66fe38066d0df7f678d00a97615cd')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"
  for builddir in python{2,3}-build-${CARCH}; do  
    rm -rf $builddir | true
    cp -r "python-${_realname}-${pkgver}" "${builddir}"
  done
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
}

build() {  
  for pver in {2,3}; do  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
      ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done
}

check() {
  for pver in {2,3}; do
    msg "Python ${pver} test for ${CARCH}"
    cd "${srcdir}/python${pver}-build-${CARCH}"
    if [ "${pver}" -eq "2" ]
    then
      PYTHONPATH=build:${PYTHONPATH} ${MINGW_PREFIX}/bin/py.test2
    else
      PYTHONPATH=build:${PYTHONPATH} ${MINGW_PREFIX}/bin/py.test
    fi
  done
}

package_python3-atomicwrites() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"
}

package_python2-atomicwrites() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING"
}

package_mingw-w64-i686-python2-atomicwrites() {
  package_python2-atomicwrites
}

package_mingw-w64-i686-python3-atomicwrites() {
  package_python3-atomicwrites
}

package_mingw-w64-x86_64-python2-atomicwrites() {
  package_python2-atomicwrites
}

package_mingw-w64-x86_64-python3-atomicwrites() {
  package_python3-atomicwrites
}
